home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / untested / tcpip / web / url.sx < prev   
Encoding:
Text File  |  1996-05-21  |  3.1 KB  |  162 lines  |  [TEXT/ttxt]

  1. --<<<<
  2.  
  3. in module WebImplementation
  4.  
  5. class url ()
  6. instance variables
  7.     scheme
  8.     withoutScheme
  9.     host
  10.     user
  11.     password
  12.     DomainName
  13.     port
  14.     path
  15.     string
  16. end
  17.  
  18. --- Simple parser for URLS
  19.  
  20. function splitstring string separator keep -> (
  21.     local n := getOrdOne string separator
  22.     local p1
  23.     local p2
  24.  
  25.     if (n == 0) then (
  26.       p1 := undefined
  27.       p2 := string
  28.     ) else (
  29.       p1 := copyFromTo string 0 (n - 1)
  30.       if keep then
  31.           p2 := copyFromTo string (n - 1) (size string)
  32.       else 
  33.           p2 := copyFromTo string n (size string)
  34.     )
  35.     #(p1, p2)
  36. )
  37.  
  38. function splitstring1 string separator keep -> (
  39.     local stuff := splitstring string separator keep
  40.     if stuff[1] = undefined do (
  41.       stuff := #(stuff[2], stuff[1])
  42.     )
  43.     stuff
  44. )
  45.  
  46. function parseURL url -> (
  47.     local scheme
  48.     local host
  49.     local stuff
  50.     local colon := ":"[1]    -- unbelievable!
  51.     local slash := "/"[1]
  52.     local host
  53.     local stuff
  54.     local withoutScheme
  55.  
  56.     stuff := splitstring url colon false
  57.     scheme := stuff[1]
  58.     withoutScheme := url := stuff[2]
  59.  
  60.     if ((url[1] == slash) and url[2] == slash) then (
  61.         url := copyFromTo url 2 (size url)
  62.         stuff := splitstring1 url slash true
  63.         host := stuff[1]
  64.         url := stuff[2]
  65.     ) else (
  66.         host := undefined
  67.     )
  68.  
  69.     #(scheme, host, url, withoutScheme)
  70. )
  71.  
  72. function parsehost host -> (
  73.     local stuff
  74.     local user := undefined
  75.     local password := undefined
  76.     local hostmachine := undefined
  77.     local port := undefined
  78.     local hoststuff
  79.  
  80.     if (host != undefined) do (
  81.      stuff := splitstring host "@"[1] false
  82.      hoststuff := stuff[2]
  83.      stuff := stuff[1]
  84.  
  85.      if stuff != undefined do (
  86.         stuff := splitstring1 stuff ":"[1] false
  87.         user := stuff[1]
  88.         password := stuff[2]
  89.      )
  90.  
  91.         stuff := splitstring1 hoststuff ":"[1] false
  92.         hostmachine := stuff[1]
  93.         port := stuff[2]
  94.     )
  95.  
  96.     #(user, password, hostmachine, port)
  97.     
  98. )
  99.  
  100. method init self {object url} #rest args #key string: str scheme: host: path: -> (
  101.     apply nextMethod self args
  102.  
  103.     if (str = unsupplied) do (
  104.       str := new String
  105.       if (scheme != undefined) do str := str + scheme + ":"
  106.       if (host != undefined) do str := str + "//" + host
  107.       if (path != undefined) do str := str + path
  108.     )
  109.  
  110.     local stuff := parseurl str
  111.     self.scheme := stuff[1]
  112.     self.host := stuff[2]
  113.     self.path := stuff[3]
  114.     self.withoutScheme := stuff[4]
  115.  
  116.     stuff := parseHost self.host
  117.  
  118.     self.user := stuff[1]
  119.     self.password := stuff[2]
  120.     self.domainName := stuff[3]
  121.     self.port := stuff[4]
  122.     self.string := str as string
  123.  
  124. )
  125.  
  126. method merge self {object URL} other -> (
  127.     if (not (isaKindof other url)) do
  128.         other := new url string: other
  129.     
  130.     local scheme := self.scheme
  131.     local host := self.host
  132.     local path := self.path
  133.  
  134.     if (scheme == undefined) do
  135.         scheme := other.scheme
  136.  
  137.     if (host == undefined) do
  138.         host := other.host
  139.  
  140.     if (path == undefined or path = "") then
  141.         path := other.path
  142.     else if (other.path != undefined and other.path != "") do
  143.         path := mergePathnames other.path path 
  144.     new url scheme: scheme host: host path: path
  145. )
  146.  
  147. function mergePathnames x y -> (
  148.     if (y[1] == "/"[1]) do return y
  149.     if (x[1] == "/"[1]) then (
  150.         for i in 1 to (size x) do (
  151.             local n := size(x) + 1 - i
  152.             if (x[n] = "/"[1]) do (
  153.                 return (copyFromTo x 0 n) + y
  154.             )
  155.         )
  156.         report 666
  157.     ) else
  158.        return y
  159. )
  160.  
  161. -->>>
  162.